<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_hotel\Entity\ShusyHotelRoom;

class HotelRoomCalendar extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_hotel_room';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyHotelRoom) {
      [$month] = $this->context;
      $inMonth = strtotime("{$month}-01");
      $value = [
        'title' => $this->formatDate($inMonth, 'Y年m月'),
        'prev' => $this->formatDate(strtotime("first day of -1 month {$month}-01"), 'Y-m'),
        'next' => $this->formatDate(strtotime("first day of +1 month {$month}-01"), 'Y-m'),
        'weeks' => $this->getWeeks($month, $this->entity),
      ];
      $this->merge($value);
    }
  }

  protected function getWeeks(string $month, ShusyHotelRoom $hotelRoom) {
    $query = db_select('shusy_hotel_line_room', 'r');
    $query->fields('r', ['id', 'occupancy']);
    $query->condition('room_id', $hotelRoom->id);
    $query->condition('uid', $this->getUid(), '<>');
    $query->orderBy('id');
    $result = $query->execute();
    $occupancies = [];
    while ($row = $result->fetchAssoc()) {
      $ymd = $this->formatDate($row['occupancy'], 'Y-m-d');
      $occupancies[$ymd][] = $row['id'];
    }

    $query = db_select('shusy_hotel_line_room', 'r');
    $query->fields('r', ['id', 'occupancy']);
    $query->condition('room_id', $hotelRoom->id);
    $query->condition('uid', $this->getUid());
    $query->orderBy('id');
    $result = $query->execute();
    $occupanciesUids = [];
    while ($row = $result->fetchAssoc()) {
      $ymd = $this->formatDate($row['occupancy'], 'Y-m-d');
      $occupanciesUids[$ymd][] = $row['id'];
    }

    $weeks = [];

    $monthStart = strtotime("first day of +0 month {$month}-01");
    $inWeek = $this->formatDate($monthStart, 'N');

    $t = $this->formatDate($monthStart, 't');

    $days = [];
    $tmp = $monthStart;
    //计算前置补充天数
    if (($inWeek - 1) > 0) {
      for ($i = 1; $i <= ($inWeek - 1); $i++) {
        $tmp -= (24 * 60 * 60);
        $days[] = [
          'type' => 0,
          'ymd' => $this->formatDate($tmp, 'Y-m-d'),
          'day' => $this->formatDate($tmp, 'j'),
        ];
      }
      if (!empty($days)) {
        $days = array_reverse($days);
      }
    }

    $week = 0;
    for ($i = 1; $i < $t; $i++) {
      if (count($days) == 7) {
        $weeks[$week] = [
          'days' => $days,
        ];
        $week++;
        $days = [];
      }
      $ymd = $this->formatDate($monthStart, 'Y-m-d');
      $days[] = [
        'type' => 1,
        'ymd' => $ymd,
        'day' => $this->formatDate($monthStart, 'j'),
        'class' => $this->getDayClass($occupancies, $occupanciesUids, $hotelRoom->rooms, $ymd),
      ];
      $monthStart += (24 * 60 * 60);
    }
    if (count($days) == 7) {
      $weeks[$week] = [
        'days' => $days,
      ];
      $week++;
      $days = [];
    }
    $days[] = [
      'type' => 1,
      'ymd' => $this->formatDate($monthStart, 'Y-m-d'),
      'day' => $this->formatDate($monthStart, 'j'),
    ];
    $week++;
    //补尾
    if (count($days) < 7) {
      for ($i = count($days); $i < 7; $i++) {
        $monthStart += (24 * 60 * 60);
        $days[] = [
          'type' => 0,
          'ymd' => $this->formatDate($monthStart, 'Y-m-d'),
          'day' => $this->formatDate($monthStart, 'j'),
        ];
      }
    }
    $weeks[$week] = ['days' => $days,];

    return $weeks;
  }

  //$monthStart % 2 == 1 ? 'close' : 'chosen',
  protected function getDayClass(array $occupancies, array $occupanciesUids, int $rooms, $ymd) {
    if (isset($occupancies[$ymd])) {
      if (count($occupancies[$ymd]) >= $rooms) {
        return 'close';
      }
    }
    if (isset($occupanciesUids[$ymd])) {
      return 'chosen';
    }
    return '';
  }
}
